package cn.kgc.kgcwxlogin.controller;

import cn.kgc.kgcwxlogin.pojo.User;
import cn.kgc.kgcwxlogin.util.AuthUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author geQiang
 * @version 1.0
 * @date 2019/4/15
 * @description cn.kgc.kgcwxlogin.controller
 */
@RestController
public class LoginContrller {

    @RequestMapping("/wxlogin")
    public void login(HttpServletResponse response) {
        System.out.println("in /WxAuth/wxLogin");
        StringBuilder url = new StringBuilder("https://open.weixin.qq.com/connect/qrconnect?");
        url.append("appid=" + AuthUtil.APPID);
        url.append("&redirect_uri=" + URLEncoder.encode(AuthUtil.REDIRECT_URI));
        url.append("&response_type=code");
        url.append("&scope=" + AuthUtil.SCOPE);
        url.append("&state=STATE#wechat_redirect");
        //重定向用户请求到微信授权URL
        try {
            response.sendRedirect(url.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/wxloginCallback")
    public String wxloginCallback(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //1. 获取微信回调请求中的code
        System.out.println("in WxAuth/callBack");
        String code = request.getParameter("code");
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                + "appid=" + AuthUtil.APPID
                + "&secret=" + AuthUtil.APPSECRET
                + "&code=" + code
                + "&grant_type=authorization_code";
        //2. 向微信发出请求，带上APPSCECRET和code，获取openid和access_toekn
        JSONObject jsonObject = AuthUtil.doGetJson(url);
        String openid = jsonObject.getString("openid");
        String token = jsonObject.getString("access_token");
        //3. 获取用户信息
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?"
                + "access_token=" + token
                + "&openid=" + openid
                + "&lang=zh_CN";
        JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
        User user = JSON.toJavaObject(userInfo, User.class);
        System.out.println(user);
        return
                "<p>name=" + user.getNickname() + "</p>" +
                        "<p>openid=" + user.getOpenid() + "</p>"
                        + "<p>sex=" + (user.getSex() == 1 ? "男" : "女") + "</p>"
                        + "<p>language=" + user.getLanguage() + "</p>"
                        + "<p>city=" + user.getCity() + "</p>"
                        + "<p>province=" + user.getProvince() + "</p>"
                        + "<p>country=" + user.getCountry() + "</p>"
                        + "<p>headimgurl=<img src='" + user.getHeadimgurl() + "'/></p>"
                        + "<p>unionid=" + user.getUnionid() + "</p>"
                        + "<p>openid=" + user.getOpenid() + "</p>"
                        + "<p>privilege=" + user.getPrivilege() + "</p>";
    }
}
